﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Interface.Imitate;
using YAPACS.RisServer.Services.ECharts;
using YAPACS.RisServer.Workflows.HomePage;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Controllers;

[Authorize]
public class HomeController(
                                IHomeChartService homeChartService,
                                IChartService chartService,
                                IGetWorkLoadChartOption getWorkLoadChartOption,
                                IHomeCardService homeCardService,
                                IGetHomeAmisView getHomeAmisView,
                                IGetHomeCardData getHomeCardData
                            ) : AbpController
{
    private readonly IHomeChartService _homeChartService = homeChartService;
    private readonly IChartService _chartService = chartService;
    private readonly IGetWorkLoadChartOption _getWorkLoadChartOption = getWorkLoadChartOption;
    private readonly IHomeCardService _homeCardService = homeCardService;
    private readonly IGetHomeAmisView _getHomeAmisView = getHomeAmisView;
    private readonly IGetHomeCardData _getHomeCardData = getHomeCardData;

    /// <summary>
    /// 获取默认开始时间
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home/getDefaultStartTime")]
    public IActionResult GetDefaultStartTime(string granularity)
    {
        var res = new AmisRes();
        if (!string.IsNullOrEmpty(granularity))
        {
            var defaultStartTime = granularity switch
            {
                "Month" => Clock.Now.Date.AddMonths(-6),// 前推半年
                "Week" => Clock.Now.Date.AddDays(-49),// 前推两个月
                "Day" => Clock.Now.Date.AddDays(-7),// 前推两周
                _ => Clock.Now.Date.AddDays(-14),// 往前推两周
            };
            res.Data = new
            {
                startTime = defaultStartTime.ToString("yyyy年MM月dd日")
            };
        }
        else
        {
            res.Status = 1;
            res.Msg = "粒度参数不能为空！";
        }
        return Json(res);
    }


    /// <summary>
    /// 获取图表配置( dataSet+config )
    /// </summary>
    /// <param name="stage">阶段</param>
    /// <param name="level">主体层级</param>
    /// <param name="startTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <param name="granularity">统计粒度</param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home/get-chart-config-and-data")]
    public async Task<IActionResult> GetChartOption()
    {
        AmisRes res = new AmisRes();
        WorkflowInput input = new WorkflowInput { { "query", HttpContext.Request.Query } };
        WorkflowOutput output = await _getWorkLoadChartOption.InvokeAsync(input);
        if (output.Success)
        {
            if (output.Data is AmisRes outPutAmisRes)
            {
                res = outPutAmisRes;
            }
            else
            {
                res.Status = 1;
                res.Msg = "工作流的数据中没有AmisRes";
            }
        }
        else
        {
            res.Status = 1;
            res.Msg = output.Error;
        }
        return Json(res);
    }

    /// <summary>
    /// 获取工作量数据集
    /// ( config )
    /// </summary>
    /// <param name="stage"></param>
    /// <param name="level"></param>
    /// <param name="startTime"></param>
    /// <param name="dateTime"></param>
    /// <param name="granularity"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home/getWorkLoadDataSet")]
    public IActionResult GetWorkLoadDataSet(string stage, string level, DateTime startTime, DateTime endTime, string granularity, string stationCode)
    {
        AmisRes res = new AmisRes();
        DataSource dataSource = _homeChartService.GetWorkLoadDataSet(stage, level, startTime, endTime, granularity, "工作量", stationCode);
        res.Data = dataSource.GetSchema();
        return Ok(res);
    }

    [HttpGet]
    [Route("api/home/get-user-info")]
    public async Task<IActionResult> GetUserInfo(string stationCode)
    {
        object result = await _chartService.GetUserInfo(stationCode);
        return Json(result);
    }


    /// <summary>
    /// 获取amis schema
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home/get-home-view")]
    public async Task<IActionResult> GetAdvanceSearchView()
    {
        AmisRes res = new();
        var workFlowOutput = await _getHomeAmisView.InvokeAsync([]);
        res.Data = new { amisJson = workFlowOutput.View };
        return Json(res);
    }


    /// <summary>
    /// 获取首页Card统计数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/home-page/get-card-data")]
    public IActionResult GetCardData()
    {
        var items = _homeCardService.GetHomeCardData();
        AmisRes res = new AmisRes { Data = new { items = items } };
        return Json(res);
    }
}
